package com.oracle.dao;

import com.oracle.dao.impl.LocationDao;
import com.oracle.mapper.LocationMapper;
import com.oracle.pojo.Location;
import com.oracle.pojo.LocationExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public class LocationDaoImpl implements LocationDao {

    @Autowired
    private LocationMapper locationMapper;

    @Override
    public Integer save(Location location) {
        try{
            int i = locationMapper.insertSelective(location);
            if (i>0){
                return 200;
            }
            return 400;
        }catch (Exception e){
            return 300;
        }
    }

    @Override
    public Integer delete(Integer id) {
        try{
            int i = locationMapper.deleteByPrimaryKey(id);
            if (i>0){
                return 200;
            }
            return 400;
        }catch (Exception e){
            return 300;
        }
    }

    @Transactional
    @Override
    public Integer update(Location location) {
        try{
            //如果需要修改的是默认地址，需要先判断数据库的该用户的地址中是否存在默认地址
            if (location.getStated()==1){
                Location one = findOne(location.getUid(),1);
                //数据库中没有，就修改信息的默认地址
                if (one == null){
                   int j =  locationMapper.updateByPrimaryKeySelective(location);
                   if (j>0){
                       return 200;
                   }else{
                       return 400;
                   }
                }else{
                    //有默认的就把他变成不是默认的地址，在修改新的默认地址
                    one.setStated(0);
                    int i = locationMapper.updateByPrimaryKeySelective(one);
                    if (i>0){
                        i = locationMapper.updateByPrimaryKeySelective(location);
                        if (i>0){
                            return 200;
                        }else {
                            return 400;
                        }
                    }
                    return 400;
                }
            }else{
                int i = locationMapper.updateByPrimaryKeySelective(location);
                if (i>0){
                    return 200;
                }
                return 400;
            }
        }catch (Exception e){
            return 300;
        }
    }

    @Override
    public List<Location> findSome(Integer uid) {
        try{
            LocationExample locationExample = new LocationExample();
            LocationExample.Criteria criteria = locationExample.createCriteria();
            List<Location> locations = null;
            if (uid!=null&&uid!=0){
                criteria.andUidEqualTo(uid);
                locations = locationMapper.selectByExample(locationExample);
            }
            return locations;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public Location findOne(Integer uid,Integer status) {
        try{
            LocationExample locationExample = new LocationExample();
            LocationExample.Criteria criteria = locationExample.createCriteria();
            List<Location> locations = null;
            if (uid!=null&&uid!=0||status!=null&&status!=0){
                criteria.andUidEqualTo(uid);
                criteria.andStatedEqualTo(status);
                locations = locationMapper.selectByExample(locationExample);
            }
            Location location = locations.isEmpty()||locations==null? null:locations.get(0);
            return location;
        }catch (Exception e){
            return null;
        }
    }
}
